<?php

namespace app\Index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;


class Soucang extends Index
{

    //增加收藏
    public function cang($id)
    {


        // var_dump($id);die;
        $this->assign('phone', Session::get('user.phone'));

        if (Session::get('user.phone')) {

            $list = Db::name('tb_users')->where('phone', Session::get('user.phone'))->select();
            $lists = $list[0];
            $jifen = Db::name('to_jifen')->where('uid', $lists['id'])->select();
            $fen = $jifen[0]['jifen'] + 10;
            $data = ['jifen' => $fen];
            $result = Db::name('to_jifen')->where('uid', $lists['id'])->update($data);


            $time = date('Y-m-d H:i:s', time());
            $list = Db::table('tb_users')->where('phone', Session::get('user.phone'))->select();
            // var_dump($list);die;
            $lists = $list[0];
            $array = Db::table('tb_musics')
                ->alias('m')
                ->join('tb_specials s', 'm.specialid = s.id')
                ->field(['m.id as mid', 's.id as sid', 's.singer', 'm.name as mname', 's.name as sname', 'm.musicaddress', 'm.img', 's.imgs'])
                ->where('m.id', $id)
                ->select();

            // var_dump($array);die;
            $arr = $array[0];

            $data = ['name' => $arr['mname'],
                'singer' => $arr['singer'],
                'mid' => $arr['mid'],
                'uid' => $lists['id'],
                'gimg' => $arr['img'],
                'specials' => $arr['sname'],
                'zimg' => $arr['imgs'],
                'musicaddress' => $arr['musicaddress']
            ];


            $list = Db::table('tb_users')->where('phone', Session::get('user.phone'))->select();
            $lists = $list[0];
            $res = Db::name('to_soucang')->where('uid', $lists['id'])->select();
            // var_dump($res);die;

            foreach ($res as $v) {
                if ($v['mid'] == $id) {
                    return $this->success('已收藏,无法收藏！！');
                }
            }

            $result = Db::name('to_soucang')->data($data)->insert();


            if ($result > 0) {
                return $this->success('收藏成功', url('index/Soucang/cha'));
            } else {
                return $this->success('收藏失败');
            }


        }

        return $this->success('请先登录', url('index/Index/index'));


    }


    public function cha()
    {


        $this->assign('phone', Session::get('user.phone'));


        $list = Db::table('tb_users')->where('phone', Session::get('user.phone'))->select();
        $lists = $list[0];
        $result = Db::name('to_soucang')->where('uid', $lists['id'])->select();
        // var_dump($result);die;

        $time = date('Y-m-d H:i:s', time());

        $this->assign('list', $lists);
        $this->assign('time', $time);
        $this->assign('result', $result);
        return $this->fetch('love/love');

    }


    //取消收藏
    public function delete($id)
    {

        $result = Db::name('to_soucang')->delete($id);

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }

        return $info;


        return $this->fetch('love/love');
    }


}
